//
//  YCFSearchBarView.m
//  UISearchBarDemo
//
//  Created by 尤春芬 on 2017/6/7.
//  Copyright © 2017年 Yuna. All rights reserved.
//

#import "YCFSearchBarView.h"
#import "Masonry.h"

@interface YCFSearchBarView ()<UITextFieldDelegate>

@property(nonatomic, strong) UITextField *searchTextFeild;

@property(nonatomic, strong) UIButton *searchBtn;

@end

@implementation YCFSearchBarView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        
        CGFloat spaLeft = 5;
        
        _searchTextFeild = [UITextField new];
        _searchTextFeild.textColor = [UIColor blackColor];
        _searchTextFeild.placeholder = @"请输入～～";
//        _searchTextFeild.textAlignment = NSTextAlignmentCenter;
        _searchTextFeild.font = [UIFont systemFontOfSize:14.0];
        _searchTextFeild.layer.borderWidth = 0.5;
        _searchTextFeild.layer.borderColor = [UIColor darkGrayColor].CGColor;
        _searchTextFeild.layer.cornerRadius = 3;
        _searchTextFeild.delegate = self;
//        _searchTextFeild.clearButtonMode=UITextFieldViewModeWhileEditing;
        
        _searchTextFeild.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, frame.size.height)];
        _searchTextFeild.leftViewMode = UITextFieldViewModeAlways;
        [self addSubview:_searchTextFeild];
        
        [_searchTextFeild mas_makeConstraints:^(MASConstraintMaker *make) {
           
//            make.left.top.equalTo(self).mas_offset(spaLeft);
//            make.right.equalTo(self.mas_right).mas_offset(-spaLeft);
//            make.bottom.equalTo(self.mas_bottom).mas_offset(-spaLeft);
            
             make.edges.equalTo(self).with.insets(UIEdgeInsetsMake(spaLeft, spaLeft, spaLeft, spaLeft));
        }];
        
        _searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_searchBtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
        [_searchBtn setImage:[UIImage imageNamed:@"searchBg"] forState:0];
        [self addSubview:_searchBtn];
        
        [_searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {

            make.centerY.equalTo(self.searchTextFeild);
            make.right.equalTo(self.mas_right).mas_offset(-spaLeft*3);
            make.width.height.mas_offset(30);
        }];

        
        
    }
    
    return self;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self endEditing:YES];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];

    self.searchSomeInfo(_searchTextFeild.text);
    return YES;
}

- (void)btnClick{
    
    self.searchSomeInfo(_searchTextFeild.text);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
